package operation;

import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;
import abbot.finder.matchers.ClassMatcher;
import abbot.tester.JComboBoxTester;
import abbot.tester.JTableHeaderLocation;
import abbot.tester.JTableTester;
import abbot.tester.JTextFieldTester;
import com.supermap.desktop.controls.ui.controls.TableRowCellEditor;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IFormTabular;
import robotCross.CrossRobot;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Hashtable;


/**
 * Created by caolp on 2016-11-11.
 */
public class TableOperation extends Operation {
	public static JDialog dialog = null;
	public JTable table;

	public TableOperation() {
		if (Application.getActiveApplication().getActiveForm() instanceof IFormTabular) {
			container = ((IFormTabular) Application.getActiveApplication().getActiveForm()).getjTableTabular();
		}
	}

	public TableOperation(JTable table) {
		this.container = table;
	}

	/**
	 * 选中某一行
	 *
	 * @param row
	 */
	public void selectRow(int row) {
		if (container != null) {
			new JTableTester().actionSelectCell(container, row, 0);
		}
	}

	/**
	 * 选中多行，ctrl
	 */
	public void selectRows(int[] row) {
		if (row == null || row.length < 1)
			return;
		if (row.length == 1) {
			this.selectRow(row[0]);
			return;
		}
		new JTableTester().click(container, row[0], 0);
		try {
			// 按住ctrl键为了多选节点
			Operation.robot.keyPress(KeyEvent.VK_CONTROL);
			for (int i = 1; i < row.length; i++) {
				this.selectRow(row[i]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放ctrl键
			Operation.robot.keyRelease(KeyEvent.VK_CONTROL);
		}
	}

	/**
	 * 选中多行，shift
	 */
	public void selectRows(int fromIndex, int endIndex) {
		this.selectRow(fromIndex);
		try {
			// 按住Shift
			Operation.robot.keyPress(KeyEvent.VK_SHIFT);
			this.selectRow(endIndex);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放Shift
			Operation.robot.keyRelease(KeyEvent.VK_SHIFT);
		}
	}

	/**
	 * 设置单元格值：comboBox,TextField
	 */
	public void setCellValue(int row, int column, Object value) {
		if (container == null)
			return;
		JTable jTable = (JTable) this.container;
		new JTableTester().actionSelectCell(container, row, column);
		super.robot.delay(500);
		new CrossRobot().doubleClick();
		Component component = jTable.getEditorComponent();
		if (component instanceof JComboBox) {
			if (value instanceof Integer) {
				new JComboBoxTester().actionSelectIndex(component, Integer.parseInt(value.toString()));
			} else {
				new JComboBoxTester().actionSelectItem(component, value.toString());
			}
		}
		if (component instanceof JTextField) {
			new JTextFieldTester().actionCommitText(component, value.toString());
			new JDialog().getComponentAt(1,2);
		}
	}

	/**
	 * 设置单元格值：comboBox,TextField
	 *
	 * @param columnName 列名
	 */
	public void setCellValue(int row, String columnName, Object value) {
		if (container == null || columnName == null)
			return;
		JTable jTable = (JTable) this.container;
		int column = new JTableHeaderLocation(columnName).getIndex(jTable.getTableHeader());
		this.setCellValue(row, column, value);
	}


	/**
	 * 选择对话框中的table行
	 * startRow：起始行，从0开始
	 * endRow：结束行
	 */
	public void clickDialogTableRow(String title, int startRow, int endRow) throws ComponentNotFoundException, MultipleComponentsFoundException {
		dialog = new DialogOperation().findDialog(title);
		Component component = new DialogOperation(title).searchComponent(new ClassMatcher(JTable.class));
		if (component instanceof JTable) {
			((JTable) component).setRowSelectionInterval(startRow, endRow);
			Operation.robot.delay(2000);
		} else {
			System.out.println("没有找到：" + ((JTable) component).getName());
		}
	}

	/**
	 * 设置对话框中的table单元格的文件类型，cell中嵌套有combox
	 * title:对话框名称
	 * fileType：文件类型
	 * row：行，从0开始
	 */
	public void setDialogTableCellValue(String title, String fileType, int row) throws ComponentNotFoundException, MultipleComponentsFoundException {
		Component component = new DialogOperation(title).searchComponent(new ClassMatcher(JTable.class));
		if (component instanceof JTable) {
			table = ((JTable) component);
			TableRowCellEditor cellEditor = (TableRowCellEditor) table.getColumn(table.getColumnName(1)).getCellEditor();
			Hashtable editors = cellEditor.getEditors();
			JComboBox comboBox = (JComboBox) ((DefaultCellEditor) editors.get(row)).getComponent();
			new JTableTester().actionSelectCell(table, row, 1);
			new Thread() {
				@Override
				public void run() {
					super.run();
					table.updateUI();
				}
			}.start();
			comboBox.setSelectedItem(fileType);
			Operation.robot.delay(2000);
		}
	}


	/**
	 * 获取属性表中指定单元格的值
	 * row：行，从0开始
	 * column：列，从0开始
	 */
	public String getTabularCellValue(int row, int column) {
		String cellValue = "";
		if (container != null) {
			cellValue = ((JTable) container).getValueAt(row, column).toString();
			return cellValue;
		}
		return cellValue;
	}


	/**
	 * 设置属性表中指定单元格的值
	 * row：行，从0开始
	 * column：列，从0开始
	 */
	public void setTabularCellValue(String cellValue, int row, int column) {
		if (container != null) {
			((JTable) container).setValueAt(cellValue, row, column);
			((JTable) container).updateUI();
			Operation.robot.delay(2000);
		}
	}

	/**
	 * 点击属性表中指定单元格
	 * row：行，从0开始
	 * column：列，从0开始
	 */
	public void clickTableCell(int row, int column) {
		if (container != null) {
			new JTableTester().actionSelectCell(container, row, column);
			Operation.robot.delay(2000);
		}
	}
}
